{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GA-LMS standard (Python 2.7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following Python 2.7 script calls the GA-LMS (standard) binary to perform a system identification task. \n",
    "\n",
    "In this example, the multivectors samples belong to the Geometric Algebra of $\\mathbb{R}^3$. Thus, each regressor and weight vector entry has 8 coefficients, i.e., for each entry of the weight vector, 8 coefficients have to be estimated. This constrasts with the usal LMS which only estimates real/complex entries. For further information, please refer to the GA documentation at <a href=\"http://www.openga.org\">www.openga.org</a>. \n",
    "\n",
    "At the end, the learning curves Mean-Square Error (MSE) and/or Excess Mean-Square Error (EMSE) are plotted."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start by importing the necessary Python modules: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Script to call the GAAFs binaries.\n",
    "#\n",
    "# Wilder Lopes - wil@openga.org\n",
    "# Dec 2015\n",
    "\n",
    "import sys, string, os\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from math import log"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The user is able to set the following AF parameters:\n",
    "\n",
    "Number of filter taps (system order): M\n",
    "\n",
    "Realizations: L\n",
    "\n",
    "Time iterations: N\n",
    "\n",
    "AF step size: mu\n",
    "\n",
    "Measurement Noise variance: sigma2v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Simulation parameters\n",
    "M       = 5 # System order\n",
    "L       = 100 # Realizations\n",
    "N       = 1000 # Time iterations\n",
    "mu      = 0.005 # AF Step size\n",
    "sigma2v = 1e-3 # Variance of measurement noise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The binary is called below using the previously set parameters. The GA-LMS runs and returns .txt files with the results: *_galms.out and *_theory.out, where * represents \"MSE\" or \"EMSE\". *_galms.out files store the ensemble-average learning curves (EMSE), while *_theory.out files store the theoretical steady-state value for MSE and EMSE.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Calling binary\n",
    "arguments = \" \" + str(M) + \" \" + str(L) + \" \" + str(N) + \" \" + str(mu)+ \\\n",
    "        \" \" + str(sigma2v)\n",
    "os.system(\"../GAAFs_source/GAAFs_standard/GA-LMS/build/GA-LMS\" + arguments)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load file MSE_galms.out and MSE_theory.out to plot MSE learning curve and theoretical curve:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f1 = open('MSE_galms.out', 'r')\n",
    "data_label1 = ['MSE_galms']\n",
    "data1_list = []\n",
    "for line in f1:\n",
    "    data1_list.append(line.rstrip('\\n'))\n",
    "\n",
    "f2 = open('MSE_theory.out', 'r')\n",
    "data_label2 = ['MSE_theory']\n",
    "data2_list = []\n",
    "for line in f2:\n",
    "    for i in range(len(data1_list)):\n",
    "        data2_list.append(line.rstrip('\\n'))\n",
    "\n",
    "data1 = [float(j) for j in data1_list] # Converts to float\n",
    "data2 = [float(j) for j in data2_list] # Converts to float\n",
    "data1_dB = [10*log(x,10) for x in data1]\n",
    "data2_dB = [10*log(x,10) for x in data2]\n",
    "\n",
    "plt.title('MSE curves')\n",
    "plt.ylabel('MSE (dB)')\n",
    "plt.xlabel('Iterations')\n",
    "plt.plot(data1_dB, label = 'MSE_galms')\n",
    "plt.plot(data2_dB, label = 'MSE_theory')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load file EMSE_galms.out and EMSE_theory.out to plot EMSE learning curve and theoretical curve:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f1 = open('EMSE_galms.out', 'r')\n",
    "data_label1 = ['EMSE_galms']\n",
    "data1_list = []\n",
    "for line in f1:\n",
    "    data1_list.append(line.rstrip('\\n'))\n",
    "\n",
    "f2 = open('EMSE_theory.out', 'r')\n",
    "data_label2 = ['EMSE_theory']\n",
    "data2_list = []\n",
    "for line in f2:\n",
    "    for i in range(len(data1_list)):\n",
    "        data2_list.append(line.rstrip('\\n'))\n",
    "\n",
    "data1 = [float(j) for j in data1_list] # Converts to float\n",
    "data2 = [float(j) for j in data2_list] # Converts to float\n",
    "data1_dB = [10*log(x,10) for x in data1]\n",
    "data2_dB = [10*log(x,10) for x in data2]\n",
    "\n",
    "plt.title('EMSE curves')\n",
    "plt.ylabel('EMSE (dB)')\n",
    "plt.xlabel('Iterations')\n",
    "plt.plot(data1_dB, label = 'EMSE_galms', color = 'r')\n",
    "plt.plot(data2_dB, label = 'EMSE_theory', color = 'magenta')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
